﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using GRWL.BZSC.DataLinq;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

namespace GRWL.BZSC.WCFLib
{
    public static class Common
    {

        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>        
        public static bool objEval(object os, object od)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    object value = item.GetValue(os, null);
                    Type t = os.GetType();
                    t.GetProperty(name).SetValue(od, value, null);
                }
                return true;
            }
            catch { }
            return false;

        }

        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>
        /// <param name="exceptNames">排除的属性成员名称</param>
        /// <returns></returns> 
        public static bool objEval(object os, object od, List<string> exceptNames)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    if (!exceptNames.Contains(name))
                    {
                        object value = item.GetValue(os, null);
                        Type t = os.GetType();
                        t.GetProperty(name).SetValue(od, value, null);
                    }
                }
                return true;
            }
            catch { }
            return false;

        }

        /// <summary>
        /// 获取option关系id序列
        /// </summary>
        /// <param name="rtn">实参</param>
        /// <param name="dc"></param>
        /// <param name="type">option类别</param>
        /// <param name="parent">上级id</param>
        public static void TrasOptions(ref ArrayList rtn, BaseDataDataContext dc, string type, int parent)
        {
            try
            {
                var childs = (from c in dc.TB_Options where c.type == type && c.parentId == parent select c.id);
                foreach (var item in childs)
                {
                    rtn.Add(item.ToString());
                    TrasOptions(ref rtn, dc, type, item);
                }
            }
            catch { }
        }

    }
}
